{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python pandas教程\n",
    "\n",
    "### pandas也是python里面很重要的做数据处理和分析的包，它里面的DataFrame是很直观的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 导入pandas包\n",
    "import pandas as pd\n",
    "# 这里引入numpy只是后面用到了，用pandas并非一定需要引入numpy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Series介绍\n",
    "\n",
    "** pandas里面的Series数据结构就和python自带的dictionary数据结构很像，只不过按照初始化的顺序，并且用表格的形式展现了出来**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     1.0\n",
      "1     3.0\n",
      "2     5.0\n",
      "3     NaN\n",
      "4    10.0\n",
      "dtype: float64\n",
      "----------\n",
      "a     1.0\n",
      "b     3.0\n",
      "c     5.0\n",
      "d     NaN\n",
      "e    10.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "s = pd.Series([1,3,5,None, 10])\n",
    "print(s)\n",
    "print('-'*10)\n",
    "sk = pd.Series([1,3,5,None, 10], index=['a','b','c','d','e'])\n",
    "print(sk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0, 3.0, 5.0, nan, 10.0, \n",
      "------------------------------\n",
      "a, b, c, d, e, \n",
      "------------------------------\n",
      "sk['a'] = 1.0\n",
      "sk['b'] = 3.0\n",
      "sk['c'] = 5.0\n",
      "sk['d'] = nan\n",
      "sk['e'] = 10.0\n",
      "------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 循环的话直接输出的是值，而不是前面的index\n",
    "for v in sk:\n",
    "    print(v, end=', ')\n",
    "print()\n",
    "print('-'*30)\n",
    "for index in sk.keys():\n",
    "    print(index, end=', ')\n",
    "print()\n",
    "print('-'*30)\n",
    "for k, v in sk.items():\n",
    "    print(\"sk['{}'] = {}\".format(k, v))\n",
    "print('-'*30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 可以通过像字典一样的方法，判断某个key是否在其中\n",
    "print('a' in sk)\n",
    "print('f' in sk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1.,   3.,   5.,  nan,  10.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换成numpy array\n",
    "sk.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a     1.0\n",
       "b     3.0\n",
       "c     5.0\n",
       "d     0.0\n",
       "e    10.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将空值填充\n",
    "sk.fillna(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame介绍\n",
    "\n",
    "** DataFrame是pandas最常用的数据结构，因为其表格化的结构很方便，和R里面的dataframe还有SQL里面的表格都是一样的，DataFrame里面有列的概念，而每一列选择出来就是Series**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          A         B         C         D\n",
      "0  0.800600 -0.290607 -1.853780 -0.368033\n",
      "1 -0.868521 -0.168274  0.541814  1.871268\n",
      "2 -1.030375 -0.758139  0.857359 -0.876903\n",
      "3  0.947127  1.243667 -0.494679  1.338379\n",
      "4 -1.485801  0.159750 -0.538269  0.668673\n",
      "5  1.851343 -1.485324 -0.043537  0.888676\n",
      "------------------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "------------------------------\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "# 生成pandas DataFrame\n",
    "df = pd.DataFrame(np.random.randn(6,4), columns=list('ABCD'))\n",
    "print(df)\n",
    "print('-'*30)\n",
    "print(type(df))\n",
    "print('-'*30)\n",
    "print(type(df['A']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['A', 'B', 'C', 'D'], dtype='object')\n",
      "------------------------------\n",
      "RangeIndex(start=0, stop=6, step=1)\n",
      "------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 返回columns和index\n",
    "print(df.columns)\n",
    "print('-'*30)\n",
    "print(df.index)\n",
    "print('-'*30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          A         B         C         D\n",
      "0  0.800600 -0.290607 -1.853780 -0.368033\n",
      "1 -0.868521 -0.168274  0.541814  1.871268\n",
      "2 -1.030375 -0.758139  0.857359 -0.876903\n",
      "3  0.947127  1.243667 -0.494679  1.338379\n",
      "4 -1.485801  0.159750 -0.538269  0.668673\n",
      "--------------------------------------------------\n",
      "          A         B         C         D\n",
      "4 -1.485801  0.159750 -0.538269  0.668673\n",
      "5  1.851343 -1.485324 -0.043537  0.888676\n"
     ]
    }
   ],
   "source": [
    "# 显示前几行和后几行\n",
    "print(df.head())\n",
    "print('-'*50)\n",
    "print(df.tail(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.035729</td>\n",
       "      <td>-0.216488</td>\n",
       "      <td>-0.255182</td>\n",
       "      <td>0.587010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.340256</td>\n",
       "      <td>0.914850</td>\n",
       "      <td>0.960287</td>\n",
       "      <td>1.036020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-1.485801</td>\n",
       "      <td>-1.485324</td>\n",
       "      <td>-1.853780</td>\n",
       "      <td>-0.876903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-0.989912</td>\n",
       "      <td>-0.641256</td>\n",
       "      <td>-0.527372</td>\n",
       "      <td>-0.108856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-0.033960</td>\n",
       "      <td>-0.229441</td>\n",
       "      <td>-0.269108</td>\n",
       "      <td>0.778675</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.910495</td>\n",
       "      <td>0.077744</td>\n",
       "      <td>0.395476</td>\n",
       "      <td>1.225953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.851343</td>\n",
       "      <td>1.243667</td>\n",
       "      <td>0.857359</td>\n",
       "      <td>1.871268</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C         D\n",
       "count  6.000000  6.000000  6.000000  6.000000\n",
       "mean   0.035729 -0.216488 -0.255182  0.587010\n",
       "std    1.340256  0.914850  0.960287  1.036020\n",
       "min   -1.485801 -1.485324 -1.853780 -0.876903\n",
       "25%   -0.989912 -0.641256 -0.527372 -0.108856\n",
       "50%   -0.033960 -0.229441 -0.269108  0.778675\n",
       "75%    0.910495  0.077744  0.395476  1.225953\n",
       "max    1.851343  1.243667  0.857359  1.871268"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据的描述\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.800600</td>\n",
       "      <td>-0.868521</td>\n",
       "      <td>-1.030375</td>\n",
       "      <td>0.947127</td>\n",
       "      <td>-1.485801</td>\n",
       "      <td>1.851343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.290607</td>\n",
       "      <td>-0.168274</td>\n",
       "      <td>-0.758139</td>\n",
       "      <td>1.243667</td>\n",
       "      <td>0.159750</td>\n",
       "      <td>-1.485324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.853780</td>\n",
       "      <td>0.541814</td>\n",
       "      <td>0.857359</td>\n",
       "      <td>-0.494679</td>\n",
       "      <td>-0.538269</td>\n",
       "      <td>-0.043537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-0.368033</td>\n",
       "      <td>1.871268</td>\n",
       "      <td>-0.876903</td>\n",
       "      <td>1.338379</td>\n",
       "      <td>0.668673</td>\n",
       "      <td>0.888676</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4         5\n",
       "A  0.800600 -0.868521 -1.030375  0.947127 -1.485801  1.851343\n",
       "B -0.290607 -0.168274 -0.758139  1.243667  0.159750 -1.485324\n",
       "C -1.853780  0.541814  0.857359 -0.494679 -0.538269 -0.043537\n",
       "D -0.368033  1.871268 -0.876903  1.338379  0.668673  0.888676"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据的转置\n",
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.851343</td>\n",
       "      <td>-1.485324</td>\n",
       "      <td>-0.043537</td>\n",
       "      <td>0.888676</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.030375</td>\n",
       "      <td>-0.758139</td>\n",
       "      <td>0.857359</td>\n",
       "      <td>-0.876903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.800600</td>\n",
       "      <td>-0.290607</td>\n",
       "      <td>-1.853780</td>\n",
       "      <td>-0.368033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.868521</td>\n",
       "      <td>-0.168274</td>\n",
       "      <td>0.541814</td>\n",
       "      <td>1.871268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.485801</td>\n",
       "      <td>0.159750</td>\n",
       "      <td>-0.538269</td>\n",
       "      <td>0.668673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.947127</td>\n",
       "      <td>1.243667</td>\n",
       "      <td>-0.494679</td>\n",
       "      <td>1.338379</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D\n",
       "5  1.851343 -1.485324 -0.043537  0.888676\n",
       "2 -1.030375 -0.758139  0.857359 -0.876903\n",
       "0  0.800600 -0.290607 -1.853780 -0.368033\n",
       "1 -0.868521 -0.168274  0.541814  1.871268\n",
       "4 -1.485801  0.159750 -0.538269  0.668673\n",
       "3  0.947127  1.243667 -0.494679  1.338379"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据按某一列排序\n",
    "df.sort_values(by='B')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame的选取 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>m</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>m</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "      <td>D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>32</td>\n",
       "      <td>m</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age gender name\n",
       "1   10      m    A\n",
       "3   20      f    B\n",
       "5   12      m    C\n",
       "7   20      f    D\n",
       "9   32      m    E"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'name': list('ABCDEFG'),\n",
    "    'age': [10, 20, 12, 20, 32, 36, 22],\n",
    "    'gender': ['m', 'f', 'm','f', 'm', 'm', 'f']\n",
    "}, index=[1,3,5,7,9,11, 13])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     A\n",
       "3     B\n",
       "5     C\n",
       "7     D\n",
       "9     E\n",
       "11    F\n",
       "13    G\n",
       "Name: name, dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取姓名一列\n",
    "df['name'] # 也可以使用df.name，两种方法一样的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "      <td>D</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age gender name\n",
       "3   20      f    B\n",
       "7   20      f    D"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取年龄为20岁，性别为f的记录\n",
    "df[(df['age']==20)&(df['gender']=='f')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    B\n",
       "7    D\n",
       "Name: name, dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取满足年龄为20岁的所有记录的name\n",
    "df.loc[df['age']==20, 'name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age       10\n",
       "gender     m\n",
       "name       A\n",
       "Name: 1, dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取第一个记录\n",
    "df.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>m</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>m</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age gender\n",
       "1   10      m\n",
       "3   20      f\n",
       "5   12      m"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取前三个记录的前两列\n",
    "df.iloc[:3, :2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame的添加和修改"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 添加新的一列 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    age gender name       city\n",
      "1    10      m    A    Beijing\n",
      "3    20      f    B  Chongqing\n",
      "5    12      m    C    Chengdu\n",
      "7    20      f    D   Shanghai\n",
      "9    32      m    E   Shandong\n",
      "11   36      m    F     Fujian\n",
      "13   22      f    G   Zhejiang\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'name': list('ABCDEFG'),\n",
    "    'age': [10, 20, 12, 20, 32, 36, 22],\n",
    "    'gender': ['m', 'f', 'm','f', 'm', 'm', 'f']\n",
    "}, index=[1,3,5,7,9,11, 13])\n",
    "# 新添加一列\n",
    "\n",
    "df['city'] = ['Beijing', 'Chongqing', 'Chengdu', 'Shanghai', 'Shandong', 'Fujian', 'Zhejiang']\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 修改满足条件的记录 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    age gender name      city\n",
      "1    10      m    A   Beijing\n",
      "3    20      f    B     Hunan\n",
      "5    12      m    C   Chengdu\n",
      "7    20      f    D     Hunan\n",
      "9    32      m    E  Shandong\n",
      "11   36      m    F    Fujian\n",
      "13   22      f    G  Zhejiang\n"
     ]
    }
   ],
   "source": [
    "# 修改年龄为20，性别为f的city为Hunan\n",
    "df.loc[(df['age']==20) & (df['gender']=='f'), 'city'] = 'Hunan'\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 对每满足条件的记录进行变换 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    age gender name      city  label  label2\n",
      "1    10      m    A   Beijing      0       0\n",
      "3    20      f    B     Hunan      0       0\n",
      "5    12      m    C   Chengdu      0       0\n",
      "7    20      f    D     Hunan      0       0\n",
      "9    32      m    E  Shandong      1       1\n",
      "11   36      m    F    Fujian      1       1\n",
      "13   22      f    G  Zhejiang      1       1\n"
     ]
    }
   ],
   "source": [
    "# 将所有年龄大于20岁的标记为1，其余的为0，并添加label一列\n",
    "df['label'] = df['age'].apply(lambda x: 1 if x > 20 else 0)\n",
    "\n",
    "# 同样也可以在df上直接使用apply\n",
    "df['label2'] = df.apply(lambda x: 1 if x['age'] > 20 else 0, axis=1)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### DataFrame的合并和新行的添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>m</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>m</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>20</td>\n",
       "      <td>f</td>\n",
       "      <td>D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>32</td>\n",
       "      <td>m</td>\n",
       "      <td>E</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>36</td>\n",
       "      <td>m</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>22</td>\n",
       "      <td>f</td>\n",
       "      <td>G</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>new</th>\n",
       "      <td>18</td>\n",
       "      <td>m</td>\n",
       "      <td>H</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     age gender name\n",
       "1     10      m    A\n",
       "3     20      f    B\n",
       "5     12      m    C\n",
       "7     20      f    D\n",
       "9     32      m    E\n",
       "11    36      m    F\n",
       "13    22      f    G\n",
       "new   18      m    H"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'name': list('ABCDEFG'),\n",
    "    'age': [10, 20, 12, 20, 32, 36, 22],\n",
    "    'gender': ['m', 'f', 'm','f', 'm', 'm', 'f']\n",
    "}, index=[1,3,5,7,9,11, 13])\n",
    "# 新添加一行\n",
    "new_record = pd.Series({\n",
    "    'name': 'H', \n",
    "    'age': 18,\n",
    "    'gender': 'm'\n",
    "}, name='new')\n",
    "df.append(new_record)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -0.192623 -0.441525  0.893663  1.365418\n",
      "1  0.866345 -1.331693 -0.393849  0.709651\n",
      "2  0.922030  0.922001  0.606191 -2.694665\n",
      "3 -1.270837  0.259524 -1.229231  1.574700\n",
      "4 -0.128226 -0.952252  1.445025  0.567271\n",
      "5 -1.419044 -0.327276 -0.863882  0.966156\n",
      "6 -0.316037  1.608719  1.577607 -0.682595\n",
      "7  0.604227  0.697976  1.402835 -0.068544\n",
      "8 -0.664805  0.897316  2.324086 -0.618642\n",
      "9  0.780297  0.217136  0.865433 -1.496809\n",
      "------------------------------------------------------------\n",
      "          0         1         2         3\n",
      "0 -0.192623 -0.441525  0.893663  1.365418\n",
      "1  0.866345 -1.331693 -0.393849  0.709651\n",
      "2  0.922030  0.922001  0.606191 -2.694665\n",
      "3 -1.270837  0.259524 -1.229231  1.574700\n",
      "4 -0.128226 -0.952252  1.445025  0.567271\n",
      "5 -1.419044 -0.327276 -0.863882  0.966156\n",
      "6 -0.316037  1.608719  1.577607 -0.682595\n",
      "7  0.604227  0.697976  1.402835 -0.068544\n",
      "8 -0.664805  0.897316  2.324086 -0.618642\n",
      "9  0.780297  0.217136  0.865433 -1.496809\n"
     ]
    }
   ],
   "source": [
    "# DataFrame的合并\n",
    "df = pd.DataFrame(np.random.randn(10, 4))\n",
    "print(df)\n",
    "print('-'*60)\n",
    "pieces = [df[:3], df[3:7], df[7:]]\n",
    "print(pd.concat(pieces))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** DataFrame的连接 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   key  lval\n",
      "0  foo     1\n",
      "1  foo     2\n",
      "------------------------------\n",
      "   key  rval\n",
      "0  foo     4\n",
      "1  foo     5\n",
      "------------------------------\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  foo     1     5\n",
       "2  foo     2     4\n",
       "3  foo     2     5"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})\n",
    "right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})\n",
    "print(left)\n",
    "print('-'*30)\n",
    "print(right)\n",
    "print('-'*30)\n",
    "pd.merge(left, right, on='key')\n",
    "# 如果连接的key在每个dataframe里面不一样，需用更复杂的参数形式\n",
    "# pd.merge(left, right, left_on=['key'], right_on=['key'], how='inner')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** DataFrame的groupby操作 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>one</td>\n",
       "      <td>0.059285</td>\n",
       "      <td>-0.918182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>one</td>\n",
       "      <td>0.843735</td>\n",
       "      <td>-0.119778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>two</td>\n",
       "      <td>1.937245</td>\n",
       "      <td>1.458306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bar</td>\n",
       "      <td>three</td>\n",
       "      <td>-1.577147</td>\n",
       "      <td>1.219092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>foo</td>\n",
       "      <td>two</td>\n",
       "      <td>0.406951</td>\n",
       "      <td>-0.973643</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bar</td>\n",
       "      <td>two</td>\n",
       "      <td>0.142249</td>\n",
       "      <td>0.426250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>foo</td>\n",
       "      <td>one</td>\n",
       "      <td>-1.414028</td>\n",
       "      <td>1.207864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>foo</td>\n",
       "      <td>three</td>\n",
       "      <td>0.878092</td>\n",
       "      <td>-0.978658</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A      B         C         D\n",
       "0  foo    one  0.059285 -0.918182\n",
       "1  bar    one  0.843735 -0.119778\n",
       "2  foo    two  1.937245  1.458306\n",
       "3  bar  three -1.577147  1.219092\n",
       "4  foo    two  0.406951 -0.973643\n",
       "5  bar    two  0.142249  0.426250\n",
       "6  foo    one -1.414028  1.207864\n",
       "7  foo  three  0.878092 -0.978658"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',\n",
    "                           'foo', 'bar', 'foo', 'foo'],\n",
    "                    'B' : ['one', 'one', 'two', 'three',\n",
    "                           'two', 'two', 'one', 'three'],\n",
    "                    'C' : np.random.randn(8),\n",
    "                    'D' : np.random.randn(8)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <td>-0.591162</td>\n",
       "      <td>1.525564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <td>1.867544</td>\n",
       "      <td>-0.204313</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            C         D\n",
       "A                      \n",
       "bar -0.591162  1.525564\n",
       "foo  1.867544 -0.204313"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('A').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bar</td>\n",
       "      <td>one</td>\n",
       "      <td>0.843735</td>\n",
       "      <td>-0.119778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>three</td>\n",
       "      <td>-1.577147</td>\n",
       "      <td>1.219092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>bar</td>\n",
       "      <td>two</td>\n",
       "      <td>0.142249</td>\n",
       "      <td>0.426250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>foo</td>\n",
       "      <td>one</td>\n",
       "      <td>-1.354744</td>\n",
       "      <td>0.289682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>foo</td>\n",
       "      <td>three</td>\n",
       "      <td>0.878092</td>\n",
       "      <td>-0.978658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>foo</td>\n",
       "      <td>two</td>\n",
       "      <td>2.344196</td>\n",
       "      <td>0.484663</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A      B         C         D\n",
       "0  bar    one  0.843735 -0.119778\n",
       "1  bar  three -1.577147  1.219092\n",
       "2  bar    two  0.142249  0.426250\n",
       "3  foo    one -1.354744  0.289682\n",
       "4  foo  three  0.878092 -0.978658\n",
       "5  foo    two  2.344196  0.484663"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['A','B'], as_index=False).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** pandas支持直接读取csv，Excel，txt，json，等很多常用的结构化的文件，这些都以 *pd.read_* 开头 **\n",
    "\n",
    "例如 (常用的encoding为utf8和gbk，gbk表示中文)\n",
    "* 读取csv文件 pd.read_csv('abc.csv', encoding='utf8')\n",
    "* 读取excel文件 pd.read_excel('abc.csv', encoding='gbk')\n",
    "\n",
    "** pandas也支持将已有的DataFrame直接输出成常用的结构化文件，这些都以 *.to_ *开头 **\n",
    "\n",
    "例如，假设我们有已经做好变换的datafame df\n",
    "* 导出csv文件 df.read_csv('abc.csv', encoding='utf8')\n",
    "* 导出excel文件 df.read_excel('abc.csv', encoding='gbk')\n",
    "\n",
    "更多的函数参数参考\n",
    "help(pd.read_csv)\n",
    "help(df.to_csv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function read_csv in module pandas.io.parsers:\n",
      "\n",
      "read_csv(filepath_or_buffer, sep=',', delimiter=None, header='infer', names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, iterator=False, chunksize=None, compression='infer', thousands=None, decimal=b'.', lineterminator=None, quotechar='\"', quoting=0, escapechar=None, comment=None, encoding=None, dialect=None, tupleize_cols=False, error_bad_lines=True, warn_bad_lines=True, skipfooter=0, skip_footer=0, doublequote=True, delim_whitespace=False, as_recarray=False, compact_ints=False, use_unsigned=False, low_memory=True, buffer_lines=None, memory_map=False, float_precision=None)\n",
      "    Read CSV (comma-separated) file into DataFrame\n",
      "    \n",
      "    Also supports optionally iterating or breaking of the file\n",
      "    into chunks.\n",
      "    \n",
      "    Additional help can be found in the `online docs for IO Tools\n",
      "    <http://pandas.pydata.org/pandas-docs/stable/io.html>`_.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    filepath_or_buffer : str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO)\n",
      "        The string could be a URL. Valid URL schemes include http, ftp, s3, and\n",
      "        file. For file URLs, a host is expected. For instance, a local file could\n",
      "        be file ://localhost/path/to/table.csv\n",
      "    sep : str, default ','\n",
      "        Delimiter to use. If sep is None, the C engine cannot automatically detect\n",
      "        the separator, but the Python parsing engine can, meaning the latter will\n",
      "        be used automatically. In addition, separators longer than 1 character and\n",
      "        different from ``'\\s+'`` will be interpreted as regular expressions and\n",
      "        will also force the use of the Python parsing engine. Note that regex\n",
      "        delimiters are prone to ignoring quoted data. Regex example: ``'\\r\\t'``\n",
      "    delimiter : str, default ``None``\n",
      "        Alternative argument name for sep.\n",
      "    delim_whitespace : boolean, default False\n",
      "        Specifies whether or not whitespace (e.g. ``' '`` or ``'    '``) will be\n",
      "        used as the sep. Equivalent to setting ``sep='\\s+'``. If this option\n",
      "        is set to True, nothing should be passed in for the ``delimiter``\n",
      "        parameter.\n",
      "    \n",
      "        .. versionadded:: 0.18.1 support for the Python parser.\n",
      "    \n",
      "    header : int or list of ints, default 'infer'\n",
      "        Row number(s) to use as the column names, and the start of the data.\n",
      "        Default behavior is as if set to 0 if no ``names`` passed, otherwise\n",
      "        ``None``. Explicitly pass ``header=0`` to be able to replace existing\n",
      "        names. The header can be a list of integers that specify row locations for\n",
      "        a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not\n",
      "        specified will be skipped (e.g. 2 in this example is skipped). Note that\n",
      "        this parameter ignores commented lines and empty lines if\n",
      "        ``skip_blank_lines=True``, so header=0 denotes the first line of data\n",
      "        rather than the first line of the file.\n",
      "    names : array-like, default None\n",
      "        List of column names to use. If file contains no header row, then you\n",
      "        should explicitly pass header=None. Duplicates in this list are not\n",
      "        allowed unless mangle_dupe_cols=True, which is the default.\n",
      "    index_col : int or sequence or False, default None\n",
      "        Column to use as the row labels of the DataFrame. If a sequence is given, a\n",
      "        MultiIndex is used. If you have a malformed file with delimiters at the end\n",
      "        of each line, you might consider index_col=False to force pandas to _not_\n",
      "        use the first column as the index (row names)\n",
      "    usecols : array-like or callable, default None\n",
      "        Return a subset of the columns. If array-like, all elements must either\n",
      "        be positional (i.e. integer indices into the document columns) or strings\n",
      "        that correspond to column names provided either by the user in `names` or\n",
      "        inferred from the document header row(s). For example, a valid array-like\n",
      "        `usecols` parameter would be [0, 1, 2] or ['foo', 'bar', 'baz'].\n",
      "    \n",
      "        If callable, the callable function will be evaluated against the column\n",
      "        names, returning names where the callable function evaluates to True. An\n",
      "        example of a valid callable argument would be ``lambda x: x.upper() in\n",
      "        ['AAA', 'BBB', 'DDD']``. Using this parameter results in much faster\n",
      "        parsing time and lower memory usage.\n",
      "    as_recarray : boolean, default False\n",
      "        DEPRECATED: this argument will be removed in a future version. Please call\n",
      "        `pd.read_csv(...).to_records()` instead.\n",
      "    \n",
      "        Return a NumPy recarray instead of a DataFrame after parsing the data.\n",
      "        If set to True, this option takes precedence over the `squeeze` parameter.\n",
      "        In addition, as row indices are not available in such a format, the\n",
      "        `index_col` parameter will be ignored.\n",
      "    squeeze : boolean, default False\n",
      "        If the parsed data only contains one column then return a Series\n",
      "    prefix : str, default None\n",
      "        Prefix to add to column numbers when no header, e.g. 'X' for X0, X1, ...\n",
      "    mangle_dupe_cols : boolean, default True\n",
      "        Duplicate columns will be specified as 'X.0'...'X.N', rather than\n",
      "        'X'...'X'. Passing in False will cause data to be overwritten if there\n",
      "        are duplicate names in the columns.\n",
      "    dtype : Type name or dict of column -> type, default None\n",
      "        Data type for data or columns. E.g. {'a': np.float64, 'b': np.int32}\n",
      "        Use `str` or `object` to preserve and not interpret dtype.\n",
      "        If converters are specified, they will be applied INSTEAD\n",
      "        of dtype conversion.\n",
      "    engine : {'c', 'python'}, optional\n",
      "        Parser engine to use. The C engine is faster while the python engine is\n",
      "        currently more feature-complete.\n",
      "    converters : dict, default None\n",
      "        Dict of functions for converting values in certain columns. Keys can either\n",
      "        be integers or column labels\n",
      "    true_values : list, default None\n",
      "        Values to consider as True\n",
      "    false_values : list, default None\n",
      "        Values to consider as False\n",
      "    skipinitialspace : boolean, default False\n",
      "        Skip spaces after delimiter.\n",
      "    skiprows : list-like or integer or callable, default None\n",
      "        Line numbers to skip (0-indexed) or number of lines to skip (int)\n",
      "        at the start of the file.\n",
      "    \n",
      "        If callable, the callable function will be evaluated against the row\n",
      "        indices, returning True if the row should be skipped and False otherwise.\n",
      "        An example of a valid callable argument would be ``lambda x: x in [0, 2]``.\n",
      "    skipfooter : int, default 0\n",
      "        Number of lines at bottom of file to skip (Unsupported with engine='c')\n",
      "    skip_footer : int, default 0\n",
      "        DEPRECATED: use the `skipfooter` parameter instead, as they are identical\n",
      "    nrows : int, default None\n",
      "        Number of rows of file to read. Useful for reading pieces of large files\n",
      "    na_values : scalar, str, list-like, or dict, default None\n",
      "        Additional strings to recognize as NA/NaN. If dict passed, specific\n",
      "        per-column NA values.  By default the following values are interpreted as\n",
      "        NaN: '', '#N/A', '#N/A N/A', '#NA', '-1.#IND', '-1.#QNAN', '-NaN', '-nan',\n",
      "        '1.#IND', '1.#QNAN', 'N/A', 'NA', 'NULL', 'NaN', 'nan'`.\n",
      "    keep_default_na : bool, default True\n",
      "        If na_values are specified and keep_default_na is False the default NaN\n",
      "        values are overridden, otherwise they're appended to.\n",
      "    na_filter : boolean, default True\n",
      "        Detect missing value markers (empty strings and the value of na_values). In\n",
      "        data without any NAs, passing na_filter=False can improve the performance\n",
      "        of reading a large file\n",
      "    verbose : boolean, default False\n",
      "        Indicate number of NA values placed in non-numeric columns\n",
      "    skip_blank_lines : boolean, default True\n",
      "        If True, skip over blank lines rather than interpreting as NaN values\n",
      "    parse_dates : boolean or list of ints or names or list of lists or dict, default False\n",
      "    \n",
      "        * boolean. If True -> try parsing the index.\n",
      "        * list of ints or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3\n",
      "          each as a separate date column.\n",
      "        * list of lists. e.g.  If [[1, 3]] -> combine columns 1 and 3 and parse as\n",
      "          a single date column.\n",
      "        * dict, e.g. {'foo' : [1, 3]} -> parse columns 1, 3 as date and call result\n",
      "          'foo'\n",
      "    \n",
      "        If a column or index contains an unparseable date, the entire column or\n",
      "        index will be returned unaltered as an object data type. For non-standard\n",
      "        datetime parsing, use ``pd.to_datetime`` after ``pd.read_csv``\n",
      "    \n",
      "        Note: A fast-path exists for iso8601-formatted dates.\n",
      "    infer_datetime_format : boolean, default False\n",
      "        If True and parse_dates is enabled, pandas will attempt to infer the format\n",
      "        of the datetime strings in the columns, and if it can be inferred, switch\n",
      "        to a faster method of parsing them. In some cases this can increase the\n",
      "        parsing speed by 5-10x.\n",
      "    keep_date_col : boolean, default False\n",
      "        If True and parse_dates specifies combining multiple columns then\n",
      "        keep the original columns.\n",
      "    date_parser : function, default None\n",
      "        Function to use for converting a sequence of string columns to an array of\n",
      "        datetime instances. The default uses ``dateutil.parser.parser`` to do the\n",
      "        conversion. Pandas will try to call date_parser in three different ways,\n",
      "        advancing to the next if an exception occurs: 1) Pass one or more arrays\n",
      "        (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the\n",
      "        string values from the columns defined by parse_dates into a single array\n",
      "        and pass that; and 3) call date_parser once for each row using one or more\n",
      "        strings (corresponding to the columns defined by parse_dates) as arguments.\n",
      "    dayfirst : boolean, default False\n",
      "        DD/MM format dates, international and European format\n",
      "    iterator : boolean, default False\n",
      "        Return TextFileReader object for iteration or getting chunks with\n",
      "        ``get_chunk()``.\n",
      "    chunksize : int, default None\n",
      "        Return TextFileReader object for iteration.\n",
      "        See the `IO Tools docs\n",
      "        <http://pandas.pydata.org/pandas-docs/stable/io.html#io-chunking>`_\n",
      "        for more information on ``iterator`` and ``chunksize``.\n",
      "    compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer'\n",
      "        For on-the-fly decompression of on-disk data. If 'infer', then use gzip,\n",
      "        bz2, zip or xz if filepath_or_buffer is a string ending in '.gz', '.bz2',\n",
      "        '.zip', or 'xz', respectively, and no decompression otherwise. If using\n",
      "        'zip', the ZIP file must contain only one data file to be read in.\n",
      "        Set to None for no decompression.\n",
      "    \n",
      "        .. versionadded:: 0.18.1 support for 'zip' and 'xz' compression.\n",
      "    \n",
      "    thousands : str, default None\n",
      "        Thousands separator\n",
      "    decimal : str, default '.'\n",
      "        Character to recognize as decimal point (e.g. use ',' for European data).\n",
      "    float_precision : string, default None\n",
      "        Specifies which converter the C engine should use for floating-point\n",
      "        values. The options are `None` for the ordinary converter,\n",
      "        `high` for the high-precision converter, and `round_trip` for the\n",
      "        round-trip converter.\n",
      "    lineterminator : str (length 1), default None\n",
      "        Character to break file into lines. Only valid with C parser.\n",
      "    quotechar : str (length 1), optional\n",
      "        The character used to denote the start and end of a quoted item. Quoted\n",
      "        items can include the delimiter and it will be ignored.\n",
      "    quoting : int or csv.QUOTE_* instance, default 0\n",
      "        Control field quoting behavior per ``csv.QUOTE_*`` constants. Use one of\n",
      "        QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3).\n",
      "    doublequote : boolean, default ``True``\n",
      "       When quotechar is specified and quoting is not ``QUOTE_NONE``, indicate\n",
      "       whether or not to interpret two consecutive quotechar elements INSIDE a\n",
      "       field as a single ``quotechar`` element.\n",
      "    escapechar : str (length 1), default None\n",
      "        One-character string used to escape delimiter when quoting is QUOTE_NONE.\n",
      "    comment : str, default None\n",
      "        Indicates remainder of line should not be parsed. If found at the beginning\n",
      "        of a line, the line will be ignored altogether. This parameter must be a\n",
      "        single character. Like empty lines (as long as ``skip_blank_lines=True``),\n",
      "        fully commented lines are ignored by the parameter `header` but not by\n",
      "        `skiprows`. For example, if comment='#', parsing '#empty\\na,b,c\\n1,2,3'\n",
      "        with `header=0` will result in 'a,b,c' being\n",
      "        treated as the header.\n",
      "    encoding : str, default None\n",
      "        Encoding to use for UTF when reading/writing (ex. 'utf-8'). `List of Python\n",
      "        standard encodings\n",
      "        <https://docs.python.org/3/library/codecs.html#standard-encodings>`_\n",
      "    dialect : str or csv.Dialect instance, default None\n",
      "        If provided, this parameter will override values (default or not) for the\n",
      "        following parameters: `delimiter`, `doublequote`, `escapechar`,\n",
      "        `skipinitialspace`, `quotechar`, and `quoting`. If it is necessary to\n",
      "        override values, a ParserWarning will be issued. See csv.Dialect\n",
      "        documentation for more details.\n",
      "    tupleize_cols : boolean, default False\n",
      "        Leave a list of tuples on columns as is (default is to convert to\n",
      "        a Multi Index on the columns)\n",
      "    error_bad_lines : boolean, default True\n",
      "        Lines with too many fields (e.g. a csv line with too many commas) will by\n",
      "        default cause an exception to be raised, and no DataFrame will be returned.\n",
      "        If False, then these \"bad lines\" will dropped from the DataFrame that is\n",
      "        returned.\n",
      "    warn_bad_lines : boolean, default True\n",
      "        If error_bad_lines is False, and warn_bad_lines is True, a warning for each\n",
      "        \"bad line\" will be output.\n",
      "    low_memory : boolean, default True\n",
      "        Internally process the file in chunks, resulting in lower memory use\n",
      "        while parsing, but possibly mixed type inference.  To ensure no mixed\n",
      "        types either set False, or specify the type with the `dtype` parameter.\n",
      "        Note that the entire file is read into a single DataFrame regardless,\n",
      "        use the `chunksize` or `iterator` parameter to return the data in chunks.\n",
      "        (Only valid with C parser)\n",
      "    buffer_lines : int, default None\n",
      "        DEPRECATED: this argument will be removed in a future version because its\n",
      "        value is not respected by the parser\n",
      "    compact_ints : boolean, default False\n",
      "        DEPRECATED: this argument will be removed in a future version\n",
      "    \n",
      "        If compact_ints is True, then for any column that is of integer dtype,\n",
      "        the parser will attempt to cast it as the smallest integer dtype possible,\n",
      "        either signed or unsigned depending on the specification from the\n",
      "        `use_unsigned` parameter.\n",
      "    use_unsigned : boolean, default False\n",
      "        DEPRECATED: this argument will be removed in a future version\n",
      "    \n",
      "        If integer columns are being compacted (i.e. `compact_ints=True`), specify\n",
      "        whether the column should be compacted to the smallest signed or unsigned\n",
      "        integer dtype.\n",
      "    memory_map : boolean, default False\n",
      "        If a filepath is provided for `filepath_or_buffer`, map the file object\n",
      "        directly onto memory and access the data directly from there. Using this\n",
      "        option can improve performance because there is no longer any I/O overhead.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    result : DataFrame or TextParser\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(pd.read_csv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method to_csv in module pandas.core.frame:\n",
      "\n",
      "to_csv(path_or_buf=None, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression=None, quoting=None, quotechar='\"', line_terminator='\\n', chunksize=None, tupleize_cols=False, date_format=None, doublequote=True, escapechar=None, decimal='.') method of pandas.core.frame.DataFrame instance\n",
      "    Write DataFrame to a comma-separated values (csv) file\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    path_or_buf : string or file handle, default None\n",
      "        File path or object, if None is provided the result is returned as\n",
      "        a string.\n",
      "    sep : character, default ','\n",
      "        Field delimiter for the output file.\n",
      "    na_rep : string, default ''\n",
      "        Missing data representation\n",
      "    float_format : string, default None\n",
      "        Format string for floating point numbers\n",
      "    columns : sequence, optional\n",
      "        Columns to write\n",
      "    header : boolean or list of string, default True\n",
      "        Write out column names. If a list of string is given it is assumed\n",
      "        to be aliases for the column names\n",
      "    index : boolean, default True\n",
      "        Write row names (index)\n",
      "    index_label : string or sequence, or False, default None\n",
      "        Column label for index column(s) if desired. If None is given, and\n",
      "        `header` and `index` are True, then the index names are used. A\n",
      "        sequence should be given if the DataFrame uses MultiIndex.  If\n",
      "        False do not print fields for index names. Use index_label=False\n",
      "        for easier importing in R\n",
      "    mode : str\n",
      "        Python write mode, default 'w'\n",
      "    encoding : string, optional\n",
      "        A string representing the encoding to use in the output file,\n",
      "        defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.\n",
      "    compression : string, optional\n",
      "        a string representing the compression to use in the output file,\n",
      "        allowed values are 'gzip', 'bz2', 'xz',\n",
      "        only used when the first argument is a filename\n",
      "    line_terminator : string, default ``'\\n'``\n",
      "        The newline character or character sequence to use in the output\n",
      "        file\n",
      "    quoting : optional constant from csv module\n",
      "        defaults to csv.QUOTE_MINIMAL. If you have set a `float_format`\n",
      "        then floats are converted to strings and thus csv.QUOTE_NONNUMERIC\n",
      "        will treat them as non-numeric\n",
      "    quotechar : string (length 1), default '\\\"'\n",
      "        character used to quote fields\n",
      "    doublequote : boolean, default True\n",
      "        Control quoting of `quotechar` inside a field\n",
      "    escapechar : string (length 1), default None\n",
      "        character used to escape `sep` and `quotechar` when appropriate\n",
      "    chunksize : int or None\n",
      "        rows to write at a time\n",
      "    tupleize_cols : boolean, default False\n",
      "        write multi_index columns as a list of tuples (if True)\n",
      "        or new (expanded format) if False)\n",
      "    date_format : string, default None\n",
      "        Format string for datetime objects\n",
      "    decimal: string, default '.'\n",
      "        Character recognized as decimal separator. E.g. use ',' for\n",
      "        European data\n",
      "    \n",
      "        .. versionadded:: 0.16.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(df.to_csv)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
